package com.djinfo.work.controller;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.entity.QsDicEntity;
import com.djinfo.work.entity.QsUserEntity;
import com.djinfo.work.service.QsDicService;
import com.djinfo.work.service.QsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by gz on 2017/6/9.
 */
@Controller
@RequestMapping("/qsdic")
public class QsDicController {
    @Autowired
    private QsDicService qsDicService;


    @ResponseBody
    @RequestMapping("/save.do")
    public AjaxJson save(QsDicEntity qs, HttpServletRequest request){
        String id = String.valueOf(request.getSession().getAttribute("userId"));
        AjaxJson ajaxJson = new AjaxJson();
        try{
            qsDicService.saveOrUpdate(qs);
            ajaxJson.setCode(101);
            ajaxJson.setMsg("success");
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    /**
     * 列表
     */
    @RequestMapping("/list.do")
    @ResponseBody
    public AjaxJson list(QsDicEntity qs, Integer pagenum, Integer pagesize){
        HashMap<String, Object> map = new HashMap<>();
        AjaxJson ajaxJson = new AjaxJson();
        List<QsDicEntity> res = qsDicService.page(qs,pagenum,pagesize);
        Integer count = qsDicService.getAllRowCount(qs);
        map.put("dataList",res);
        map.put("total",count);
        ajaxJson.setCode(101);
        ajaxJson.setMsg("success");
        ajaxJson.setResult(map);
        return ajaxJson;
    }

    /**
     * 获取对象信息
     */
    @RequestMapping("/info.do")
    public ModelAndView info(QsDicEntity qs){
        HashMap<String, Object> map = new HashMap<>();
        AjaxJson ajaxJson = new AjaxJson();
        QsDicEntity res = qsDicService.selectOne(qs);
        map.put("data",res);
        ajaxJson.setCode(101);
        ajaxJson.setMsg("success");
        ajaxJson.setResult(map);
        return new ModelAndView("qs_user/edit.html","map",map);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public AjaxJson delete(QsDicEntity qs){

        AjaxJson ajaxJson = new AjaxJson();
        qsDicService.delete(qs);
        ajaxJson.setCode(101);
        ajaxJson.setMsg("success");
        return ajaxJson;
    }
}
